const express = require('express')
const router = express.Router()
const mysql = require("../mysql/config")
let connection = null
const resSend = require('../utils/resSend')

router.all("/", (req, res, next) => {
    if (req.user.role != 1) {
        resSend(res, 500, "权限不足！", Date.now())
    } else {
        next()
    }
})

router.post('/allUser', (req, res) => {
    connection = mysql.createConnection()
    connection.connect()
    connection.query('select * from user_table', (err, result) => {
        if (err) {
            resSend(res, 500, '查询失败')
        } else {
            resSend(res, 200, '查询成功', result)
        }
    })
})

// 删除/恢复 用户
router.post('/userDel', (req, res) => {
    connection = mysql.createConnection()
    connection.connect()
    connection.query('update user_table set isDel=? where id=?', [req.body.status, req.body.id], (err, result) => {
        if (err) {
            resSend(res, 500, '操作失败！')
        } else {
            resSend(res, 200, '操作成功！')
        }
    })
})


// 获取用户信息
router.post('/admin/userInfo', (req, res) => {
    connection = mysql.createConnection()
    connection.connect()
    connection.query('SELECT uImage,uName FROM user_table where id=?', [req.body.id], (err, result) => {
        if (err) {
            resSend(res, 500, '操作失败！')
        } else {
            resSend(res, 200, '操作成功！', result[0])
        }
    })
})

// 获取用户信息
router.post('/userInfo', (req, res) => {
    connection = mysql.createConnection()
    connection.connect()
    connection.query('SELECT * FROM user_table where id=?', [req.user.id], (err, result) => {
        if (err) {
            resSend(res, 500, '操作失败！')
        } else {
            resSend(res, 200, '操作成功！', result[0])
        }
    })
})

module.exports = router